package com.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.dto.SupplierOrders;
import com.community.mapper.SupplierMapper;
import com.community.pojo.Product;
import com.community.pojo.Supplier;
import com.community.service.ISupplierService;
import com.community.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public PageInfo<Supplier> getSupplierList(int pageNo, int pageSize, Supplier supplier) {
        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "C001-1");

        // 添加额外的查询条件
        if (supplier.getCompanyName() != null) {
            wrapper.eq("company_name", supplier.getCompanyName());
        }
        if (supplier.getName() != null) {
            wrapper.eq("name", supplier.getName());
        }
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        // 查询语句
        List<Supplier> suppliers = this.supplierMapper.queryAll(supplier);
        // 封装分页结果
        return new PageInfo<>(suppliers);
    }

    @Override
    public PageUtils<Supplier> supplierFuHePage( int pageNo, int pageSize,Supplier supplier) {
        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        // 额外加2个条件
        // 审核状态: 待审核
        wrapper.eq("check_tag", "S001-1");

        if (supplier != null) {
            if (supplier.getCompanyName() != null && !supplier.getCompanyName().isEmpty()) {
                wrapper.like("company_name", supplier.getCompanyName());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<Supplier> suppliers = this.list(wrapper);

        PageInfo<Supplier> pageInfo = new PageInfo<>(suppliers);

        // 把PageHelper的PageInfo转换为自定义的PageUtils对象
        PageUtils<Supplier> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public void delBySupplierId(Supplier supplier) {
        if (supplier != null && supplier.getSupplierId() != null) {
            // 构建更新的值和条件
            UpdateWrapper<Supplier> updateWrapper = new UpdateWrapper<>();

            // 设置要更新的列和新值
            updateWrapper.set("is_deleted", 1)
                    .eq("supplier_id", supplier.getSupplierId())
                    .eq("is_deleted", 0);;

            // 执行更新操作
            this.update(updateWrapper);
        }
    }

    //查询所有的采购单
    @Override
    public PageUtils<Supplier> searchAll(int pageNo, int pageSize) {

        PageHelper.startPage(pageNo, pageSize);
        List<Supplier> list = this.list();
        PageInfo<Supplier> pageInfo = new PageInfo<>(list);
        // PageInfo 转换成 PageUtils
        PageUtils<Supplier> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;

    }

    @Override
    public List<SupplierOrders> supplierBySupplierId(Integer id) {
        return supplierMapper.supplierByConsumerId(id);
    }
}
